package com.blindBox.front.controller.login;

import com.blindBox.front.commonResult.MsgBackToFront;
import com.blindBox.front.handler.loginHandler.AbstractLoginHandler;
import com.blindBox.front.handler.loginHandler.loginHandlerImpl.LoginByAccountHandler;
import com.blindBox.front.handler.loginHandler.loginHandlerImpl.LoginByEamilHandler;
import com.blindBox.front.handler.loginHandler.loginHandlerImpl.LoginByTokenHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

//用户登录控制层
@RestController
public class UserLoginController
{
    //处理账号登录的类
     @Resource(name="loginByAccountHandler")
    private AbstractLoginHandler handleLoginByAccount;
    //处理token登录的类
    @Resource(name="loginByTokenHandler")
    private AbstractLoginHandler handlerLoginByToken;
    //处理邮箱登录类
    @Resource(name="loginByTokeneEamil")
    private AbstractLoginHandler loginByTokeneEamil;
    //账号密码登录
    @PostMapping("/u_login_account")
    public MsgBackToFront uLoginByAccount(@RequestParam("uname") String uname,
                                          @RequestParam("upwd") String upwd)
    {
        LoginByAccountHandler handleLoginByAccount = (LoginByAccountHandler)this.handleLoginByAccount;
        return handleLoginByAccount.autoWiredUser(uname,upwd).login();
    }

    //token登录
    @PostMapping("/u_login_token")
    public MsgBackToFront uLoginByToken(@RequestParam("token")String token)
    {
       LoginByTokenHandler loginByTokenHandler = (LoginByTokenHandler)this.handlerLoginByToken;
        return loginByTokenHandler.parseTokenToUser(token).login();
    }

    //发送验证码
    @PostMapping("/u_send_code")
    public MsgBackToFront uLoginByEamil(@RequestParam("email")String email)
    {
        LoginByEamilHandler loginByTokeneEamil = (LoginByEamilHandler) this.loginByTokeneEamil;
        return loginByTokeneEamil.sendCode(email);
    }


    //处理邮箱登录的请求
    @PostMapping("/u_login_email")
    public MsgBackToFront uLoginByEamil(@RequestParam("email")String email,@RequestParam("code")String code)
    {
        LoginByEamilHandler loginByTokeneEamil = (LoginByEamilHandler) this.loginByTokeneEamil;
        return loginByTokeneEamil.verify_eamil_login(email,code);
    }



    //检验token是否合法
    @PostMapping("/u_token_verify")
    public MsgBackToFront verifyToken(@RequestParam("token")String token)
    {
        LoginByTokenHandler loginByTokenHandler = (LoginByTokenHandler)this.handlerLoginByToken;
        return loginByTokenHandler.verifyToken(token);
    }

    //查询当前用户的id
    @PostMapping("/get_u_info")
    public MsgBackToFront getUid(@RequestParam("uname")String uname)
    {
        LoginByAccountHandler handleLoginByAccount = (LoginByAccountHandler)this.handleLoginByAccount;
        return handleLoginByAccount.getUInfo(uname);
    }

}
